const {Worker, isMainThread, parentPort, workerData} = require('worker_threads');

if(isMainThread){
    console.log("__filename==", __filename)

    // new Worker(__filename);
    const worker1 = new Worker(__filename, {
        workerData:{"b":"adfdf"}
    });
    worker1.on("message", (result)=>{
        console.log("worker ",worker1.threadId,result)
    })
    worker1.stdout.on('data', function (data) {
        // console.log('worker1 stdout: ' + data);  // 输出到控制台
        console.log("worker ",worker1.threadId, ""+data)
    });
    const worker2 = new Worker(__filename, {
        workerData:{"a":13232}
    });

    worker2.on("message", (result)=>{
        // console.log("worker2 ==",result)
        console.log("worker ",worker2.threadId,result)
    })

    worker2.stdout.on('data', function (data) {
        // console.log('worker2 stdout: ' + data);  // 输出到控制台
        console.log("worker ",worker2.threadId,""+data)
    });
    
    worker2.stderr.on('data', function (data) {
        // console.log('worker2 stdout: ' + data);  // 输出到控制台
        console.log("worker ",worker2.threadId,""+data)
    });
    const worker3 = new Worker(__filename, {
        workerData:33
    });

    worker3.on("message", (result)=>{
        // console.log("worker3 ==", result)
        console.log("worker ",worker3.threadId,result)
    })
    worker3.stdout.on('data', function (data) {
        // console.log('worker3 stdout: ' + data);  // 输出到控制台
        console.log("worker ",worker3.threadId,""+data)
    });
}else{
    function fib(n){
        if(n === 0){
            return 0;
        }else if(n === 1){
            return 1;
        }else{
            return fib(n-1)+fib(n-2)
        }
    }
    console.log("workerData==isMainThread=", isMainThread)
    console.log("workerData===", workerData)
    // const result = fib(workerData)
    const result = workerData
    // console.log("result===", result)
    // parentPort.postMessage(result);
}

